package com.uzai.console.service.device.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.uzai.common.dto.device.*;
import com.uzai.common.enums.DeviceTypeEnum;
import com.uzai.common.enums.RedisCacheKeyEnum;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.common.utils.EsIndexName;
import com.uzai.console.common.utils.DateUtil;
import com.uzai.console.common.utils.Tools;
import com.uzai.console.constant.Const;
import com.uzai.console.constant.ES.ElasticsearchUtilGroupMsg;
import com.uzai.console.dto.device.DeviceGroupQuery;
import com.uzai.console.dto.device.DeviceQuery;
import com.uzai.console.dto.wechatprivate.app.DeviceAppInfoQuery;
import com.uzai.console.dto.wechatprivate.devicewechatinfo.DeviceWechatInfoQuery;
import com.uzai.console.dto.wechatwork.devicewxworkinfo.DeviceWxworkInfoQuery;
import com.uzai.console.dto.wxpubmanage.WxpubInfoListQuery;
import com.uzai.console.entity.*;
import com.uzai.console.entity.es.EsWorkWxGroupMember;
import com.uzai.console.entity.es.EsWxGroupMember;
import com.uzai.console.entity.wxpubmanage.WxpubInfoEntity;
import com.uzai.console.enums.DeviceProductEnum;
import com.uzai.console.mapper.*;
import com.uzai.console.service.device.DeviceService;
import com.uzai.console.service.merchant.MerchantService;
import com.uzai.console.vo.device.DeviceInfoVo;
import com.uzai.console.vo.wechatprivate.devicewechatinfo.DeviceWechatInfoVo;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/***
 * @author liuqi
 * @date 2021年08月25日 20:18
 */
@Service
public class DeviceServiceImpl implements DeviceService {

    @Autowired
    private DeviceWechatInfoMapper deviceWechatInfoMapper;
    @Autowired
    private DeviceGroupMapper deviceGroupMapper;
    @Autowired
    private DeviceWxworkInfoMapper deviceWxworkInfoMapper;
    @Autowired
    private ConfigPlanMapper configPlanMapper;
    @Autowired
    private WxpubInfoMapper wxpubInfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private ElasticsearchUtilGroupMsg elasticsearchUtilGroupMsg;
    @Autowired
    private DeviceTiktokInfoMapper deviceTiktokInfoMapper;
    @Autowired
    private DeviceAppInfoMapper deviceAppInfoMapper;

    /**
     * 根据设备类型和设备唯一ID查询设备信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceUniqueId
     * @return null
     */
    public Device findDeviceInfoByDeviceUniqueId(Long deviceUniqueId, String deviceType, Long merId){
        Device device = null;

        if(deviceUniqueId == null){
            return device;
        }

        if(StringUtils.isNotBlank(deviceType)) {
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)) {
                DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectById(deviceUniqueId, merId);
                if (deviceWechatInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceWechatInfo, device);
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    if(deviceWechatInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoPcWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoPcWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoPcWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)) { //企业微信号
                DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoMapper.selectById(deviceUniqueId, merId);
                if (deviceWxworkInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceWxworkInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(deviceWxworkInfo.getRecvmsgType());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);

                    if(deviceWxworkInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)) { //公众号
                WxpubInfoEntity wxpubInfoEntity = wxpubInfoMapper.queryObject(deviceUniqueId);
                if (wxpubInfoEntity != null) {
                    device = new Device();
                    BeanUtils.copyProperties(wxpubInfoEntity, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.WX_PUB.getId());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()));
                    if(object != null) {
                        DeviceInfoWxpubCacheDto deviceInfoWxpubCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoWxpubCacheDto.class);
                        //缓存不为空
                        if(deviceInfoWxpubCacheDto != null){
                            //获取最新心跳时间
                            Integer lastHeartBeatTime = deviceInfoWxpubCacheDto.getLastHeartBeatTime();
                            if(lastHeartBeatTime != null){
                                //判断在线（少于5分钟表示在线）
                                if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                    device.setOnline(1); //在线
                                }
                            }
                        }
                    }
                }
            }else if (DeviceTypeEnum.TIKTOK.getKey().equals(deviceType)) { //抖音设备
                DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectById(deviceUniqueId, merId);
                if (deviceTiktokInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceTiktokInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.TIKTOK.getId());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_TIKTOK.getKey(), deviceTiktokInfo.getId()));
                    if(object != null) {
                        DeviceInfoTiktokCacheDto deviceInfoTiktokCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoTiktokCacheDto.class);
                        //缓存不为空
                        if(deviceInfoTiktokCacheDto != null){
                            //获取最新心跳时间
                            Integer lastHeartBeatTime = deviceInfoTiktokCacheDto.getLastHeartBeatTime();
                            if(lastHeartBeatTime != null){
                                //判断在线（少于5分钟表示在线）
                                if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                    device.setOnline(1); //在线
                                }
                            }
                        }
                    }
                }
            }else if (DeviceTypeEnum.APP.getKey().equals(deviceType)) { //app设备
                DeviceAppInfo deviceAppInfo = deviceAppInfoMapper.selectById(deviceUniqueId, merId);
                if (deviceAppInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceAppInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.APP.getId());
                    //在线状态
                    //是否在线，默认为在线
                    device.setOnline(1);
                }
            }
        }else{
            //1-先查询个人微信机器人
            DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectById(deviceUniqueId, merId);
            if (deviceWechatInfo != null) {
                device = new Device();
                BeanUtils.copyProperties(deviceWechatInfo, device);
                //在线状态
                //是否在线，默认为离线
                device.setOnline(0);
                if(deviceWechatInfo.getRecvmsgType() != null){
                    //PC端
                    if(deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT_VALUE){
                        Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
                        if(object_mobile != null) {
                            DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoPcWeChatCacheDto.class);
                            //PC端缓存不为空
                            if(deviceInfoPcWeChatCacheDto != null){
                                //获取最新心跳时间
                                Integer lastHeartBeatTime = deviceInfoPcWeChatCacheDto.getLastHeartBeatTime();
                                if(lastHeartBeatTime != null){
                                    //判断在线（少于5分钟表示在线）
                                    if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                        device.setOnline(1); //在线
                                    }
                                }
                            }
                        }
                    }else{ //移动端
                        Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
                        if(object_mobile != null) {
                            DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWeChatCacheDto.class);
                            //PC端缓存不为空
                            if(deviceInfoMobileWeChatCacheDto != null){
                                //获取最新心跳时间
                                Integer lastHeartBeatTime = deviceInfoMobileWeChatCacheDto.getLastHeartBeatTime();
                                if(lastHeartBeatTime != null){
                                    //判断在线（少于5分钟表示在线）
                                    if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                        device.setOnline(1); //在线
                                    }
                                }
                            }
                        }
                    }
                }
                return device;
            }

            //企业微信号
            DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoMapper.selectById(deviceUniqueId, merId);
            if (deviceWxworkInfo != null) {
                device = new Device();
                BeanUtils.copyProperties(deviceWxworkInfo, device);
                //赋值处理消息deviceTypeId
                device.setRecvmsgType(deviceWxworkInfo.getRecvmsgType());
                //在线状态
                //是否在线，默认为离线
                device.setOnline(0);
                if(deviceWxworkInfo.getRecvmsgType() != null){
                    //PC端
                    if(deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT_VALUE){
                        Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
                        if(object_mobile != null) {
                            DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoWxWorkCacheDto.class);
                            //PC端缓存不为空
                            if(deviceInfoWxWorkCacheDto != null){
                                //获取最新心跳时间
                                Integer lastHeartBeatTime = deviceInfoWxWorkCacheDto.getLastHeartBeatTime();
                                if(lastHeartBeatTime != null){
                                    //判断在线（少于5分钟表示在线）
                                    if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                        device.setOnline(1); //在线
                                    }
                                }
                            }
                        }
                    }else{ //移动端
                        Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
                        if(object_mobile != null) {
                            DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWxWorkCacheDto.class);
                            //PC端缓存不为空
                            if(deviceInfoMobileWxWorkCacheDto != null){
                                //获取最新心跳时间
                                Integer lastHeartBeatTime = deviceInfoMobileWxWorkCacheDto.getLastHeartBeatTime();
                                if(lastHeartBeatTime != null){
                                    //判断在线（少于5分钟表示在线）
                                    if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                        device.setOnline(1); //在线
                                    }
                                }
                            }
                        }
                    }
                }

                return device;
            }

            //公众号
            WxpubInfoEntity wxpubInfoEntity = wxpubInfoMapper.queryObject(deviceUniqueId);
            if (wxpubInfoEntity != null) {
                device = new Device();
                BeanUtils.copyProperties(wxpubInfoEntity, device);
                //赋值处理消息deviceTypeId
                device.setRecvmsgType(DeviceTypeEnum.WX_PUB.getId());
                //在线状态
                //是否在线，默认为离线
                device.setOnline(0);
                Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()));
                if(object != null) {
                    DeviceInfoWxpubCacheDto deviceInfoWxpubCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoWxpubCacheDto.class);
                    //缓存不为空
                    if(deviceInfoWxpubCacheDto != null){
                        //获取最新心跳时间
                        Integer lastHeartBeatTime = deviceInfoWxpubCacheDto.getLastHeartBeatTime();
                        if(lastHeartBeatTime != null){
                            //判断在线（少于5分钟表示在线）
                            if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                device.setOnline(1); //在线
                            }
                        }
                    }
                }
            }

            //抖音
            DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectById(deviceUniqueId, merId);
            if (deviceTiktokInfo != null) {
                device = new Device();
                BeanUtils.copyProperties(deviceTiktokInfo, device);
                //赋值处理消息deviceTypeId
                device.setRecvmsgType(DeviceTypeEnum.TIKTOK.getId());
                //在线状态
                //是否在线，默认为离线
                device.setOnline(0);
                Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_TIKTOK.getKey(), deviceTiktokInfo.getId()));
                if(object != null) {
                    DeviceInfoTiktokCacheDto deviceInfoTiktokCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoTiktokCacheDto.class);
                    //缓存不为空
                    if(deviceInfoTiktokCacheDto != null){
                        //获取最新心跳时间
                        Integer lastHeartBeatTime = deviceInfoTiktokCacheDto.getLastHeartBeatTime();
                        if(lastHeartBeatTime != null){
                            //判断在线（少于5分钟表示在线）
                            if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                device.setOnline(1); //在线
                            }
                        }
                    }
                }
                return device;
            }

            //APP
            DeviceAppInfo deviceAppInfo = deviceAppInfoMapper.selectById(deviceUniqueId, merId);
            if (deviceAppInfo != null) {
                device = new Device();
                BeanUtils.copyProperties(deviceAppInfo, device);
                //赋值处理消息deviceTypeId
                device.setRecvmsgType(DeviceTypeEnum.APP.getId());
                //在线状态
                //是否在线，默认为在线
                device.setOnline(1);
                return device;
            }

        }
        return device;
    }

    @Override
    public List<Device> findDeviceInfoByDeviceUniqueIds(List<Long> deviceUniqueId, String deviceType, Long merId) {

        List<Device> result = new ArrayList<>();
        for (Long item : deviceUniqueId) {
            Device deviceInfoByDeviceUniqueId = findDeviceInfoByDeviceUniqueId(item, deviceType, merId);
            if (Objects.isNull(deviceInfoByDeviceUniqueId)) {
                continue;
            }
            result.add(deviceInfoByDeviceUniqueId);
        }
        return result;
    }

    /***
     * 根据设备类型和设备ID查询设备信息
     * @author liuqi
     * @date 2021/8/25 11:38
     * @param deviceType
     * @return null
     */
    public Device findDeviceInfoByDeviceId(String deviceId, String deviceType){
        Device device = null;
        if(deviceId != null && StringUtils.isNotBlank(deviceType)) {
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceType)) {
                DeviceWechatInfo deviceWechatInfo = deviceWechatInfoMapper.selectByDeviceId(deviceId);
                if (deviceWechatInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceWechatInfo, device);
                    if(deviceWechatInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoPcWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoPcWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoPcWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceType)) { //企业微信号
                DeviceWxworkInfo deviceWxworkInfo = deviceWxworkInfoMapper.selectByDeviceId(deviceId);
                if (deviceWxworkInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceWxworkInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(deviceWxworkInfo.getRecvmsgType());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    if(deviceWxworkInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            device.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } else if (DeviceTypeEnum.WX_PUB.getKey().equals(deviceType)) { //公众号
                WxpubInfoEntity wxpubInfoEntity = wxpubInfoMapper.queryObjectByDeviceId(deviceId);
                if (wxpubInfoEntity != null) {
                    device = new Device();
                    BeanUtils.copyProperties(wxpubInfoEntity, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.WX_PUB.getId());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXPUB.getKey(), wxpubInfoEntity.getId()));
                    if(object != null) {
                        DeviceInfoWxpubCacheDto deviceInfoWxpubCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoWxpubCacheDto.class);
                        //缓存不为空
                        if(deviceInfoWxpubCacheDto != null){
                            //获取最新心跳时间
                            Integer lastHeartBeatTime = deviceInfoWxpubCacheDto.getLastHeartBeatTime();
                            if(lastHeartBeatTime != null){
                                //判断在线（少于5分钟表示在线）
                                if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                    device.setOnline(1); //在线
                                }
                            }
                        }
                    }
                }
            }else if (DeviceTypeEnum.TIKTOK.getKey().equals(deviceType)) { //抖音设备
                DeviceTiktokInfo deviceTiktokInfo = deviceTiktokInfoMapper.selectByDeviceId(deviceId);
                if (deviceTiktokInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceTiktokInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.TIKTOK.getId());
                    //在线状态
                    //是否在线，默认为离线
                    device.setOnline(0);
                    Object object = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_TIKTOK.getKey(), deviceTiktokInfo.getId()));
                    if(object != null) {
                        DeviceInfoTiktokCacheDto deviceInfoTiktokCacheDto = JSONObject.parseObject(String.valueOf(object), DeviceInfoTiktokCacheDto.class);
                        //缓存不为空
                        if(deviceInfoTiktokCacheDto != null){
                            //获取最新心跳时间
                            Integer lastHeartBeatTime = deviceInfoTiktokCacheDto.getLastHeartBeatTime();
                            if(lastHeartBeatTime != null){
                                //判断在线（少于5分钟表示在线）
                                if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                    device.setOnline(1); //在线
                                }
                            }
                        }
                    }
                }
            }else if (DeviceTypeEnum.APP.getKey().equals(deviceType)) { //APP设备
                DeviceAppInfo deviceAppInfo = deviceAppInfoMapper.selectByDeviceId(deviceId);
                if (deviceAppInfo != null) {
                    device = new Device();
                    BeanUtils.copyProperties(deviceAppInfo, device);
                    //赋值处理消息deviceTypeId
                    device.setRecvmsgType(DeviceTypeEnum.APP.getId());
                    //在线状态
                    //是否在线，默认为在线
                    device.setOnline(1);
                }
            }
        }
        return device;
    }

    /***
     * 查询未绑定客服账号的机器人列表-分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public Page<DeviceInfoVo> findDevicePageForUnBindKefuUser(DeviceQuery deviceQuery){
        //返回到页面分页对象
        Page<DeviceInfoVo> pageVo = new Page<>();
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        if(StringUtils.isNotBlank(deviceQuery.getDeviceType())){
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceQuery.getDeviceType())) {
                DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
                BeanUtils.copyProperties(deviceQuery, deviceWechatInfoQuery);
                //执行分页查询对象
                Page<DeviceWechatInfo> page = new Page<>();
                deviceWechatInfoQuery.convert(page);
                List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByListForUnBindKefuUser(page, deviceWechatInfoQuery);
                BeanUtils.copyProperties(page, pageVo);
                if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
                    for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                        DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                        BeanUtils.copyProperties(deviceWechatInfo, deviceInfoVo);

                        //个人微信机器人
                        deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());

                        deviceInfoVoPageVoList.add(deviceInfoVo);
                    }
                }
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceQuery.getDeviceType())) { //企业微信号
                DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
                BeanUtils.copyProperties(deviceQuery, deviceWxworkInfoQuery);
                //执行分页查询对象
                Page<DeviceWxworkInfo> page = new Page<>();
                deviceWxworkInfoQuery.convert(page);
                List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByListForUnBindKefuUser(page, deviceWxworkInfoQuery);
                BeanUtils.copyProperties(page, pageVo);
                if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
                    for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                        DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                        BeanUtils.copyProperties(deviceWxworkInfo, deviceInfoVo);

                        //企业微信机器人
                        deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());

                        deviceInfoVoPageVoList.add(deviceInfoVo);
                    }
                }
            } else if (DeviceTypeEnum.WX_PUB.getKey().equals(deviceQuery.getDeviceType())) {//公众号
                WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
                BeanUtils.copyProperties(deviceQuery, wxpubInfoListQuery);
                //执行分页查询对象
                Page<WxpubInfoEntity> page = new Page<>();
                wxpubInfoListQuery.convert(page);
                List<WxpubInfoEntity> deviceWxworkInfoList = wxpubInfoMapper.queryList(page, wxpubInfoListQuery);
                BeanUtils.copyProperties(page, pageVo);
                if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
                    for (WxpubInfoEntity wxpubInfoEntity : deviceWxworkInfoList) {
                        DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                        BeanUtils.copyProperties(wxpubInfoEntity, deviceInfoVo);
                        //企业微信机器人
                        deviceInfoVo.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
                        deviceInfoVoPageVoList.add(deviceInfoVo);
                    }
                }
            }
        }
        pageVo.setRecords(deviceInfoVoPageVoList);
        //将分页信息复制到返回到页面的分页MO当中
        return pageVo;
    }

    /***
     * 查询未绑定客服账号的机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceListForUnBindKefuUser(DeviceQuery deviceQuery){
        if(StringUtils.isNotBlank(deviceQuery.getDeviceType())){
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceQuery.getDeviceType())) {
                return findDeviceWechatInfoListForUnBindKefuUser(deviceQuery);
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceQuery.getDeviceType())) { //企业微信号
                return findDeviceWxworkInfoListForUnBindKefuUser(deviceQuery);
            } else if (DeviceTypeEnum.WX_PUB.getKey().equals(deviceQuery.getDeviceType())) { //公众号
                return findDeviceWxpubInfoListForUnBindKefuUser(deviceQuery);
            }else{
                return null;
            }
        }else{
            //新建返回到页面的LIST对象
            List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
            //添加个人微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceWechatInfoListForUnBindKefuUser(deviceQuery));
            //添加企业微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceWxworkInfoListForUnBindKefuUser(deviceQuery));
            //添加公众号机器人
            return deviceInfoVoPageVoList;
        }
    }

    /***
     * 查询机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceList(DeviceQuery deviceQuery){

        if(StringUtils.isNotBlank(deviceQuery.getDeviceType())) {
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceQuery.getDeviceType())) {
                return findDeviceList_private(deviceQuery);
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceQuery.getDeviceType())) {//企业微信号
                return findDeviceList_work(deviceQuery);
            } else if (DeviceTypeEnum.WX_PUB.getKey().equals(deviceQuery.getDeviceType())) { //公众号
                return findDeviceList_wxpub(deviceQuery);
            }else {
                return null;
            }
        }else{//类型为空，查询所有
            //新建返回到页面的LIST对象
            List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
            //添加个人微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceList_private(deviceQuery));
            //添加企业微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceList_work(deviceQuery));
            //添加公众号微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceList_wxpub(deviceQuery));
            //添加公众号机器人
            return deviceInfoVoPageVoList;
        }
    }

    /**
     * 查询机器人列表-分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public Page<Device> findDevicePage(DeviceQuery deviceQuery){
        Page<Device> page_device = new Page<>();
        deviceQuery.convert(page_device);
        List<Device> deviceList = new ArrayList<>();
        page_device.setRecords(deviceList);
        //设备类型为空
        if(StringUtils.isBlank(deviceQuery.getDeviceType())){
            return page_device;
        }

        //个人微信
        if(deviceQuery.getDeviceType().equals(DeviceTypeEnum.DT_PC_WECHAT.getKey())){
            //执行分页查询对象
            Page<DeviceWechatInfo> page_wechat = new Page<>();
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(deviceQuery, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setMerId(deviceQuery.getMerId());
            deviceWechatInfoQuery.convert(page_wechat);
            List<DeviceWechatInfo> deviceWechatInfoList =  deviceWechatInfoMapper.findByList(page_wechat, deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                    Device device = new Device();
                    BeanUtils.copyProperties(deviceWechatInfo, device);
                    deviceList.add(device);
                }
            }
            //企业微信
        }else if(deviceQuery.getDeviceType().equals(DeviceTypeEnum.DT_WORK_WECHAT.getKey())){
            //执行分页查询对象
            Page<DeviceWxworkInfo> page_wxwork = new Page<>();
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(deviceQuery, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setMerId(deviceQuery.getMerId());
            deviceWxworkInfoQuery.convert(page_wxwork);
            List<DeviceWxworkInfo> deviceWxworkInfoList =  deviceWxworkInfoMapper.findByList(page_wxwork, deviceWxworkInfoQuery);
            if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                    Device device = new Device();
                    BeanUtils.copyProperties(deviceWxworkInfo, device);
                    deviceList.add(device);
                }
            }
        }
        return page_device;
    }


    /***
     * 查询个人微信机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceList_private(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
        BeanUtils.copyProperties(deviceQuery, deviceWechatInfoQuery);
        List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
        if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
            for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(deviceWechatInfo, deviceInfoVo);

                //个人微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());

                //分组名称
                if(deviceWechatInfo.getGroupId() != null){
                    DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWechatInfo.getGroupId(), deviceWechatInfo.getMerId());
                    if(deviceGroup != null){
                        deviceInfoVo.setGroupName(deviceGroup.getName());
                    }
                }
                //配置方案名称
                if(deviceWechatInfo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceWechatInfo.getConfigId(), deviceWechatInfo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }
                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }


    /***
     * 查询企业微信机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceList_work(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
        BeanUtils.copyProperties(deviceQuery, deviceWxworkInfoQuery);
        //执行分页查询对象
        Page<DeviceWxworkInfo> page = new Page<>();
        deviceWxworkInfoQuery.convert(page);
        List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
        if (deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
            for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(deviceWxworkInfo, deviceInfoVo);

                //企业微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());

                //分组名称
                if(deviceWxworkInfo.getGroupId() != null){
                    DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWxworkInfo.getGroupId(), deviceWxworkInfo.getMerId());
                    if(deviceGroup != null){
                        deviceInfoVo.setGroupName(deviceGroup.getName());
                    }
                }
                //配置方案名称
                if(deviceWxworkInfo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceWxworkInfo.getConfigId(), deviceWxworkInfo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                //赋值处理消息deviceTypeId
                deviceInfoVo.setRecvmsgType(DeviceTypeEnum.DT_WORK_WECHAT.getId());

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询app机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceList_app(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        DeviceAppInfoQuery deviceAppInfoQuery = new DeviceAppInfoQuery();
        BeanUtils.copyProperties(deviceQuery, deviceAppInfoQuery);
        //执行分页查询对象
        Page<DeviceWxworkInfo> page = new Page<>();
        deviceAppInfoQuery.convert(page);
        List<DeviceAppInfo> deviceAppInfoList = deviceAppInfoMapper.findByList(deviceAppInfoQuery);
        if (deviceAppInfoList != null && deviceAppInfoList.size() > 0) {
            for (DeviceAppInfo deviceAppInfo : deviceAppInfoList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(deviceAppInfo, deviceInfoVo);
                //APP机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.APP.getKey());

                //分组名称
                if(deviceAppInfo.getGroupId() != null){
                    DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceAppInfo.getGroupId(), deviceAppInfo.getMerId());
                    if(deviceGroup != null){
                        deviceInfoVo.setGroupName(deviceGroup.getName());
                    }
                }
                //配置方案名称
                if(deviceAppInfo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceAppInfo.getConfigId(), deviceAppInfo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                //赋值处理消息deviceTypeId
                deviceInfoVo.setRecvmsgType(DeviceTypeEnum.APP.getId());

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询公众号机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceList_wxpub(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
        BeanUtils.copyProperties(deviceQuery, wxpubInfoListQuery);
        //执行分页查询对象
        Page<WxpubInfoEntity> page = new Page<>();
        wxpubInfoListQuery.convert(page);
        List<WxpubInfoEntity> wxpubInfoEntityList = wxpubInfoMapper.queryList(wxpubInfoListQuery);
        if (wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0) {
            for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(wxpubInfoEntity, deviceInfoVo);

                //公众号微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());

                //配置方案名称
                if(deviceInfoVo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceInfoVo.getConfigId(), deviceInfoVo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                //赋值处理消息deviceTypeId
                deviceInfoVo.setRecvmsgType(DeviceTypeEnum.WX_PUB.getId());

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询未绑定客服账号的个人微信机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceWechatInfoListForUnBindKefuUser(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
        BeanUtils.copyProperties(deviceQuery, deviceWechatInfoQuery);
        List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByListForUnBindKefuUser(deviceWechatInfoQuery);
        if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
            for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(deviceWechatInfo, deviceInfoVo);
                //个人微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());

                //分组名称
                if(deviceWechatInfo.getGroupId() != null){
                    DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWechatInfo.getGroupId(),deviceWechatInfo.getMerId());
                    if(deviceGroup != null){
                        deviceInfoVo.setGroupName(deviceGroup.getName());
                    }
                }
                //配置方案名称
                if(deviceWechatInfo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceWechatInfo.getConfigId(), deviceWechatInfo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询未绑定客服账号的企业微信机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceWxworkInfoListForUnBindKefuUser(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
        BeanUtils.copyProperties(deviceQuery, deviceWxworkInfoQuery);
        List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByListForUnBindKefuUser(deviceWxworkInfoQuery);
        if(deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
            for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(deviceWxworkInfo, deviceInfoVo);
                //企业微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());

                //分组名称
                if(deviceWxworkInfo.getGroupId() != null){
                    DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWxworkInfo.getGroupId(), deviceWxworkInfo.getMerId());
                    if(deviceGroup != null){
                        deviceInfoVo.setGroupName(deviceGroup.getName());
                    }
                }

                //配置方案名称
                if(deviceWxworkInfo.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(deviceWxworkInfo.getConfigId(), deviceWxworkInfo.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询未绑定客服账号的公众号微信机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceWxpubInfoListForUnBindKefuUser(DeviceQuery deviceQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
        WxpubInfoListQuery wxpubInfoListQuery = new WxpubInfoListQuery();
        BeanUtils.copyProperties(deviceQuery, wxpubInfoListQuery);
        List<WxpubInfoEntity> wxpubInfoEntityList = wxpubInfoMapper.findByListForUnBindKefuUser(wxpubInfoListQuery);
        if(wxpubInfoEntityList != null && wxpubInfoEntityList.size() > 0) {
            for (WxpubInfoEntity wxpubInfoEntity : wxpubInfoEntityList) {
                DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                BeanUtils.copyProperties(wxpubInfoEntity, deviceInfoVo);
                //公众号微信机器人
                deviceInfoVo.setDeviceType(DeviceTypeEnum.WX_PUB.getKey());
                //配置方案名称
                if(wxpubInfoEntity.getConfigId() != null){
                    ConfigPlan configPlan = configPlanMapper.selectById(wxpubInfoEntity.getConfigId(), wxpubInfoEntity.getMerId());
                    if(configPlan != null){
                        deviceInfoVo.setConfigName(configPlan.getName());
                    }
                }

                deviceInfoVoPageVoList.add(deviceInfoVo);
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询群内机器人列表-不分页
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupQuery
     * @return null
     */
    public List<DeviceInfoVo> findDeviceListByGroup(DeviceGroupQuery deviceGroupQuery){

        if(StringUtils.isBlank(deviceGroupQuery.getGroupWxid())){
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "微信群wxid不能为空");
        }

        if(StringUtils.isNotBlank(deviceGroupQuery.getDeviceType())){
            //个人微信设备
            if (DeviceTypeEnum.DT_PC_WECHAT.getKey().equals(deviceGroupQuery.getDeviceType())) {
                return findDeviceListByGroup_private_new(deviceGroupQuery);
            } else if (DeviceTypeEnum.DT_WORK_WECHAT.getKey().equals(deviceGroupQuery.getDeviceType())) {//企业微信号
                return findDeviceListByGroup_work_new(deviceGroupQuery);
            }else {
                return null;
            }
        }else{//类型为空，查询所有
            //新建返回到页面的LIST对象
            List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();
            //添加个人微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceListByGroup_private_new(deviceGroupQuery));
            //添加企业微信机器人
            deviceInfoVoPageVoList.addAll(findDeviceListByGroup_work_new(deviceGroupQuery));
            //添加公众号机器人
            return deviceInfoVoPageVoList;
        }
    }

    /***
     * 查询群内个人微信机器人列表-不分页（该方法废除，因为微信群成员无法区分是机器人还是会员）
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceListByGroup_private(DeviceGroupQuery deviceGroupQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();

        //封装个人微信成员Es中所有的机器人微信id
        List<String> deviceIdList = new ArrayList<>();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_and = QueryBuilders.boolQuery();
        //运营商ID
        boolQuery_and.must(QueryBuilders.termQuery("merId", Tools.getLong(deviceGroupQuery.getMerId())));
        //微信群
        boolQuery_and.must(QueryBuilders.termQuery("groupWxid", deviceGroupQuery.getGroupWxid()));
        //微信查询机器人type=1
        boolQuery_and.must(QueryBuilders.termQuery("type", 1));
        searchSourceBuilder.query(boolQuery_and);
        //通过查询运营商其他索引分片获取分片索引
        String uzai_wx_group_member = EsIndexName.UZAI_WX_GROUP_MEMBER;
        uzai_wx_group_member = merchantService.getMechantOtherEsIndexName(uzai_wx_group_member, deviceGroupQuery.getMerId());
        List<Map<String,Object>> dataList = elasticsearchUtilGroupMsg.scrollSearchAll(uzai_wx_group_member, searchSourceBuilder, deviceGroupQuery.getMerId());
        if(dataList != null && dataList.size() > 0) {
            for (Map<String, Object> dataMp : dataList) {
                EsWxGroupMember esWxGroupMember = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), EsWxGroupMember.class);
                deviceIdList.add(esWxGroupMember.getWxid());
            }
        }

        //群内找到机器人，则在去机器人表中查询机器人授权信息
        if(deviceIdList != null && deviceIdList.size() > 0){
            //新建返回到页面的LIST对象
            DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
            BeanUtils.copyProperties(deviceGroupQuery, deviceWechatInfoQuery);
            deviceWechatInfoQuery.setDeviceIdList(deviceIdList);//封装群成员Es中的机器人微信id
            //查询机器人类型（0-查询群发机器人；1-查询托机器人）
            Integer type = Tools.getInteger(deviceGroupQuery.getType());
            if(type.intValue() == 0){//查询群内发单机器人
                deviceWechatInfoQuery.setProductCode(DeviceProductEnum.FD.getValue());
                deviceWechatInfoQuery.setExpireType(1);//未到期
            }else{//查询群内气氛组机器人
                deviceWechatInfoQuery.setProductCode(DeviceProductEnum.TUO.getValue());
                deviceWechatInfoQuery.setExpireType(1);//未到期
            }
            List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
            if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
                for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                    DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                    BeanUtils.copyProperties(deviceWechatInfo, deviceInfoVo);

                    //个人微信机器人
                    deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());

                    //分组名称
                    if(deviceWechatInfo.getGroupId() != null){
                        DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWechatInfo.getGroupId(), deviceWechatInfo.getMerId());
                        if(deviceGroup != null){
                            deviceInfoVo.setGroupName(deviceGroup.getName());
                        }
                    }
                    //配置方案名称
                    if(deviceWechatInfo.getConfigId() != null){
                        ConfigPlan configPlan = configPlanMapper.selectById(deviceWechatInfo.getConfigId(), deviceWechatInfo.getMerId());
                        if(configPlan != null){
                            deviceInfoVo.setConfigName(configPlan.getName());
                        }
                    }

                    //是否在线(默认离线)
                    deviceInfoVo.setOnline(0);//默认离线
                    if(deviceWechatInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoPcWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoPcWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoPcWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                    deviceInfoVoPageVoList.add(deviceInfoVo);
                }
            }
        }

        return deviceInfoVoPageVoList;
    }

    /***
     * 查询群内个人微信机器人列表-不分页
     * 先查询
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceListByGroup_private_new(DeviceGroupQuery deviceGroupQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();

        //新建返回到页面的LIST对象
        DeviceWechatInfoQuery deviceWechatInfoQuery = new DeviceWechatInfoQuery();
        BeanUtils.copyProperties(deviceGroupQuery, deviceWechatInfoQuery);
        //查询机器人类型（0-查询群发机器人；1-查询托机器人）
        Integer type = Tools.getInteger(deviceGroupQuery.getType());
        if(type.intValue() == 0){//查询群内发单机器人
            deviceWechatInfoQuery.setProductCode(DeviceProductEnum.FD.getValue());
            deviceWechatInfoQuery.setExpireType(1);//未到期
        }else{//查询群内气氛组机器人
            deviceWechatInfoQuery.setProductCode(DeviceProductEnum.TUO.getValue());
            deviceWechatInfoQuery.setExpireType(1);//未到期
        }

        List<DeviceWechatInfo> deviceWechatInfoList = deviceWechatInfoMapper.findByList(deviceWechatInfoQuery);
        if(deviceWechatInfoList != null && deviceWechatInfoList.size() > 0) {
            //通过查询运营商其他索引分片获取分片索引
            String uzai_wx_group_member = EsIndexName.UZAI_WX_GROUP_MEMBER;
            uzai_wx_group_member = merchantService.getMechantOtherEsIndexName(uzai_wx_group_member, deviceGroupQuery.getMerId());

            for (DeviceWechatInfo deviceWechatInfo : deviceWechatInfoList) {
                //判断此群是是否包含此机器人成员
                //群成员id(运营商Id_群wxid_成员wxid)
                String id_tmp = deviceGroupQuery.getMerId() + "_" + deviceGroupQuery.getGroupWxid() + "_" + deviceWechatInfo.getDeviceId();
                Map<String,Object> dataMp_member_tmp = elasticsearchUtilGroupMsg.searchDataById(uzai_wx_group_member, id_tmp, null);
                EsWxGroupMember esWxGroupMember_tmp  = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp_member_tmp)), EsWxGroupMember.class);
                //是否为空，不为空且状态必须在线，表示机器人是该群的成员
                if(esWxGroupMember_tmp != null && esWxGroupMember_tmp.getStatus() == 1){
                    DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                    BeanUtils.copyProperties(deviceWechatInfo, deviceInfoVo);
                    //个人微信机器人
                    deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_PC_WECHAT.getKey());
                    //是否在线(默认离线)
                    deviceInfoVo.setOnline(0);//默认离线
                    if(deviceWechatInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWechatInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_PC_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_PC.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoPcWeChatCacheDto deviceInfoPcWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoPcWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoPcWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoPcWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WECHAT_MOBLE.getKey(), deviceWechatInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWeChatCacheDto deviceInfoMobileWeChatCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWeChatCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWeChatCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWeChatCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }
                    deviceInfoVoPageVoList.add(deviceInfoVo);
                }
            }
        }
        return deviceInfoVoPageVoList;
    }

    /***
     * 查询群内企业微信机器人列表-不分页（已经废除，因为企业微信群成员无法区分是机器人还是会员）
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceListByGroup_work(DeviceGroupQuery deviceGroupQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();

        //封装个人微信成员Es中所有的机器人微信id
        List<String> deviceIdList = new ArrayList<>();
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        BoolQueryBuilder boolQuery_and = QueryBuilders.boolQuery();
        //运营商ID
        boolQuery_and.must(QueryBuilders.termQuery("merId", Tools.getLong(deviceGroupQuery.getMerId())));
        //微信群
        boolQuery_and.must(QueryBuilders.termQuery("groupWxid", deviceGroupQuery.getGroupWxid()));
        //微信查询机器人type=1
        boolQuery_and.must(QueryBuilders.termQuery("type", 1));
        searchSourceBuilder.query(boolQuery_and);
        //通过查询运营商其他索引分片获取分片索引
        String uzai_workwx_group_member = EsIndexName.UZAI_WORKWX_GROUP_MEMBER;
        uzai_workwx_group_member = merchantService.getMechantOtherEsIndexName(uzai_workwx_group_member, deviceGroupQuery.getMerId());
        List<Map<String,Object>> dataList = elasticsearchUtilGroupMsg.scrollSearchAll(uzai_workwx_group_member, searchSourceBuilder, deviceGroupQuery.getMerId());
        if(dataList != null && dataList.size() > 0) {
            for (Map<String, Object> dataMp : dataList) {
                EsWorkWxGroupMember esWorkWxGroupMember = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp)), EsWorkWxGroupMember.class);
                deviceIdList.add(esWorkWxGroupMember.getWxid());
            }
        }

        //查询到群内机器人，再去机器人表中查询
        if(dataList != null && dataList.size() > 0){
            DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
            BeanUtils.copyProperties(deviceGroupQuery, deviceWxworkInfoQuery);
            deviceWxworkInfoQuery.setDeviceIdList(deviceIdList);//封装群成员Es中的机器人微信id

            //查询机器人类型（0-查询群发机器人；1-查询托机器人）
            Integer type = Tools.getInteger(deviceGroupQuery.getType());
            if(type.intValue() == 0){//查询群内发单机器人
                deviceWxworkInfoQuery.setProductCode(DeviceProductEnum.FD.getValue());
                deviceWxworkInfoQuery.setExpireType(1);//未到期
            }else{//查询群内气氛组机器人
                deviceWxworkInfoQuery.setProductCode(DeviceProductEnum.TUO.getValue());
                deviceWxworkInfoQuery.setExpireType(1);//未到期
            }

            //执行分页查询对象
            List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
            if (deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {
                for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                    DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                    BeanUtils.copyProperties(deviceWxworkInfo, deviceInfoVo);

                    //企业微信机器人
                    deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());

                    //分组名称
                    if(deviceWxworkInfo.getGroupId() != null){
                        DeviceGroup deviceGroup = deviceGroupMapper.selectById(deviceWxworkInfo.getGroupId(), deviceWxworkInfo.getMerId());
                        if(deviceGroup != null){
                            deviceInfoVo.setGroupName(deviceGroup.getName());
                        }
                    }
                    //配置方案名称
                    if(deviceWxworkInfo.getConfigId() != null){
                        ConfigPlan configPlan = configPlanMapper.selectById(deviceWxworkInfo.getConfigId(), deviceWxworkInfo.getMerId());
                        if(configPlan != null){
                            deviceInfoVo.setConfigName(configPlan.getName());
                        }
                    }

                    //在线状态
                    //是否在线，默认为离线
                    deviceInfoVo.setOnline(0);
                    if(deviceWxworkInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }

                    deviceInfoVoPageVoList.add(deviceInfoVo);
                }
            }
        }

        return deviceInfoVoPageVoList;
    }

    /***
     * 查询群内企业微信机器人列表-不分页（已经废除，因为企业微信群成员无法区分是机器人还是会员）
     * @author liuqi
     * @date 2021/8/13 11:38
     * @param deviceGroupQuery
     * @return null
     */
    private List<DeviceInfoVo> findDeviceListByGroup_work_new(DeviceGroupQuery deviceGroupQuery){
        //新建返回到页面的LIST对象
        List<DeviceInfoVo> deviceInfoVoPageVoList = new ArrayList<>();

        DeviceWxworkInfoQuery deviceWxworkInfoQuery = new DeviceWxworkInfoQuery();
        BeanUtils.copyProperties(deviceGroupQuery, deviceWxworkInfoQuery);

        //查询机器人类型（0-查询群发机器人；1-查询托机器人）
        Integer type = Tools.getInteger(deviceGroupQuery.getType());
        if(type.intValue() == 0){//查询群内发单机器人
            deviceWxworkInfoQuery.setProductCode(DeviceProductEnum.FD.getValue());
            deviceWxworkInfoQuery.setExpireType(1);//未到期
        }else{//查询群内气氛组机器人
            deviceWxworkInfoQuery.setProductCode(DeviceProductEnum.TUO.getValue());
            deviceWxworkInfoQuery.setExpireType(1);//未到期
        }
        //执行分页查询对象
        List<DeviceWxworkInfo> deviceWxworkInfoList = deviceWxworkInfoMapper.findByList(deviceWxworkInfoQuery);
        if (deviceWxworkInfoList != null && deviceWxworkInfoList.size() > 0) {

            //通过查询运营商其他索引分片获取分片索引
            String uzai_workwx_group_member = EsIndexName.UZAI_WORKWX_GROUP_MEMBER;
            uzai_workwx_group_member = merchantService.getMechantOtherEsIndexName(uzai_workwx_group_member, deviceGroupQuery.getMerId());

            for (DeviceWxworkInfo deviceWxworkInfo : deviceWxworkInfoList) {
                //id为merId_group_群Wxid_成员wxid
                String id_tmp = deviceGroupQuery.getMerId() + "_" + deviceGroupQuery.getGroupWxid() + "_" + deviceWxworkInfo.getDeviceId();
                Map<String,Object> dataMp_member_tmp = elasticsearchUtilGroupMsg.searchDataById(uzai_workwx_group_member, id_tmp, null);
                EsWorkWxGroupMember esWorkWxGroupMember_tmp  = JSONObject.parseObject(Tools.getStrEmpty(JSONObject.toJSONString(dataMp_member_tmp)), EsWorkWxGroupMember.class);
                //是否为空，不为空且在线状态，表示机器人是该群的成员
                if(esWorkWxGroupMember_tmp != null && esWorkWxGroupMember_tmp.getStatus() == 1){
                    DeviceInfoVo deviceInfoVo = new DeviceInfoVo();
                    BeanUtils.copyProperties(deviceWxworkInfo, deviceInfoVo);
                    //企业微信机器人
                    deviceInfoVo.setDeviceType(DeviceTypeEnum.DT_WORK_WECHAT.getKey());
                    //在线状态
                    //是否在线，默认为离线
                    deviceInfoVo.setOnline(0);

                    if(deviceWxworkInfo.getRecvmsgType() != null){
                        //PC端
                        if(deviceWxworkInfo.getRecvmsgType().intValue() == DeviceTypeEnum.DT_WORK_WECHAT_VALUE){
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoWxWorkCacheDto deviceInfoWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }else{ //移动端
                            Object object_mobile = redisTemplate.opsForValue().get(String.format(RedisCacheKeyEnum.DEVICE_INFO_WXWORK_MOBLE.getKey(), deviceWxworkInfo.getId()));
                            if(object_mobile != null) {
                                DeviceInfoMobileWxWorkCacheDto deviceInfoMobileWxWorkCacheDto = JSONObject.parseObject(String.valueOf(object_mobile), DeviceInfoMobileWxWorkCacheDto.class);
                                //PC端缓存不为空
                                if(deviceInfoMobileWxWorkCacheDto != null){
                                    //获取最新心跳时间
                                    Integer lastHeartBeatTime = deviceInfoMobileWxWorkCacheDto.getLastHeartBeatTime();
                                    if(lastHeartBeatTime != null){
                                        //判断在线（少于5分钟表示在线）
                                        if(DateUtil.getNowTime() - lastHeartBeatTime  < Const.Device.HEART_BEAT_OFF_LINE_TIME){
                                            deviceInfoVo.setOnline(1); //在线
                                        }
                                    }
                                }
                            }
                        }
                    }

                    deviceInfoVoPageVoList.add(deviceInfoVo);
                }
            }
        }
        return deviceInfoVoPageVoList;
    }

}
